﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using GSSoftSL.Models;
using GSSoftSL.ViewModel;
using GSSoftSL.Web.Models;

namespace GSSoftSL.Views
{
    /// <summary>
    /// Description for TruongView.
    /// </summary>
    public partial class GiaoVienView : UserControl
    {
        private ListBox _lsb;
        //private TruongEditView child = new TruongEditView();

        /// <summary>
        /// Initializes a new instance of the TruongView class.
        /// </summary>
        public GiaoVienView()
        {
            InitializeComponent();
            //child.DataContext = this.DataContext;
            //this.Resources[]
        }

        private void dataForm_CurrentItemChanged(object sender, EventArgs e)
        {
            //dataForm
            if (dataForm.CurrentItem == null)
                return;
            GiaoVienViewModel context = this.DataContext as GiaoVienViewModel;
            if (context == null || context.Mons == null)
                return;
            GiaoVien gv = dataForm.CurrentItem as GiaoVien;
            if (gv == null)
                return;
            if (gv.GiaoVienMons.Count == 0)
            {
                foreach (var item in context.Mons)
                    item.IsChecked = false;
            }
            else
            {
                //string[] options = context.Mons.Select(n => n.Key.ToString()).ToArray();
                string[] mons = gv.GiaoVienMons.Select(n => n.MaMon).ToArray();
                foreach (var item in context.Mons)
                {
                    if (mons.Contains(item.Key))
                        item.IsChecked = true;
                    else
                        item.IsChecked = false;
                }
            }
            
        }

        private void dataForm_ContentLoaded(object sender, DataFormContentLoadEventArgs e)
        {
            if (dataForm.Mode == DataFormMode.AddNew)
            {
                GiaoVien gv = dataForm.CurrentItem as GiaoVien;
                if (gv == null)
                    return;
                gv.MaGV = "";
            }
        }

        private void ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            _lsb = sender as ListBox;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //dataForm.
            GiaoVien gv = dataForm.CurrentItem as GiaoVien;
            if (gv == null)
                return;
            if (dataForm.Mode == DataFormMode.Edit && gv.EntityState == EntityState.Unmodified)
            {
                if (gv.GiaoVienMons.Count == 0)
                    gv.Flag = true;
                else
                {
                    CheckBox cb = sender as CheckBox;
                    if (cb == null)
                        return;
                    CheckBoxContext context = cb.DataContext as CheckBoxContext;
                    if (context == null)
                        return;
                    if (!gv.GiaoVienMons.Select(n => n.MaMon).Contains(context.Key))
                        gv.Flag = true;
                }
            }
            //gv.EntityState = 
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            GiaoVien gv = dataForm.CurrentItem as GiaoVien;
            if (gv == null)
                return;
            if (dataForm.Mode == DataFormMode.Edit && gv.EntityState == EntityState.Unmodified)
            {
                CheckBox cb = sender as CheckBox;
                if (cb == null)
                    return;
                CheckBoxContext context = cb.DataContext as CheckBoxContext;
                if (context == null)
                    return;
                if (gv.GiaoVienMons.Select(n => n.MaMon).Contains(context.Key))
                    gv.Flag = true;
            }
        }

        private void dataForm_EditEnding(object sender, DataFormEditEndingEventArgs e)
        {
            if (e.EditAction == DataFormEditAction.Cancel)
                return;
            GiaoVien gv = dataForm.CurrentItem as GiaoVien;
            if (gv == null)
                return;
            if (gv.GiaoVienID != ((GiaoVienViewModel)this.DataContext).Current.GiaoVienID)
                return;
            string[] monsSelected =
                (_lsb.ItemsSource as CheckBoxContext[]).Where(n => n.IsChecked).Select(n => n.Key.ToString()).ToArray();
            if (gv.EntityState == EntityState.New)
            {
                gv.GiaoVienID = Guid.NewGuid();
                foreach (string mon in monsSelected)
                    gv.GiaoVienMons.Add(new GiaoVienMon() {GiaoVienId = gv.GiaoVienID, MaMon = mon});
            }
            else
            {
                string[] mons = gv.GiaoVienMons.Select(n => n.MaMon).ToArray();

                var added = monsSelected.Except(mons).ToArray();
                var removed = mons.Except(monsSelected).ToArray();
                if (added.Count() > 0)
                    foreach (string mon in added)
                        gv.GiaoVienMons.Add(new GiaoVienMon() { GiaoVienId = gv.GiaoVienID, MaMon = mon });
                if (removed.Count() > 0)
                    foreach (string mon in removed)
                        gv.GiaoVienMons.Remove(gv.GiaoVienMons.Single(n => n.MaMon == mon));
            }
        }
    }
}